package com.djinfo.work.common.service.impl;

import com.djinfo.work.common.dao.impl.BaseDaoImpl;
import com.djinfo.work.util.PageSet;
import org.springframework.beans.factory.annotation.Autowired;
import com.djinfo.work.common.service.BaseService;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * hutu 2017/6/13
 */
//@Service("BaseService")
public class BaseServiceImpl<T extends Serializable,K extends BaseDaoImpl> implements BaseService<T>{
    @Autowired
    public K dao;

    public void save(T obj){dao.save(obj);}
    @Override
    public void delete(T entiy) {
        dao.delete(entiy);
    }

    @Override
    public void update(T entity) {
        dao.update(entity);
    }

    @Override
    public Object getByPrimaryKey(Class object, String id) {
        return dao.getByPrimaryKey(object, id);
    }

    @Override
    public <T> T getEntity(Class entityName, Serializable id) {
        return dao.getEntity(entityName, id);
    }

    @Override
    public <T> List<T> loadAll(Class<T> entityClass) {
        return dao.loadAll(entityClass);
    }

    @Override
    public void updateBySqlString(String query) {
        dao.updateBySqlString(query);
    }

    @Override
    public void delSome(String hql) {
        dao.delSome(hql);
    }

    @Override
    public PageSet queryPage(T entity, Integer pageNum, Integer pageSize) {
        PageSet pageSet = new PageSet();
        String hql = " from "+entity.getClass().getSimpleName()+" where 1=1 ";
        String hql2 = " select count(*) from "+entity.getClass().getSimpleName()+" where 1=1 ";

        pageSet.setList(dao.pageQuery( hql,  pageNum,  pageSize));
        pageSet.setAllRow(dao.getUniqueObject(hql2).hashCode());
        return pageSet;
    }

    @Override
    public List<T> queryList(T entity) {
        String hql = " from "+entity.getClass().getSimpleName()+" where 1=1 ";
        return (List<T>)dao.getList(hql);
    }

    @Override
    public void saveList(List<T> list) {
        dao.saveList(list);
    }

    @Override
    public <T> T findUniqueByProperty(Class<T> entityClass,
                                      String propertyName, Object value) {
        return dao.findUniqueByProperty(entityClass, propertyName, value);
    }

    @Override
    public <T> List<T> findByProperty(Class<T> entityClass, String propertyName, Object value) {
        return dao.findByProperty(entityClass,propertyName,value);
    }


}
